import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RoleSelectFrame extends JFrame {

    public RoleSelectFrame() {
        setTitle("河南工业大学运动会管理系统 - 角色选择");
        setSize(400, 250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 粉色背景
        getContentPane().setBackground(new Color(255, 182, 193));

        // 标题
        JLabel title = new JLabel("请选择登录身份");
        title.setFont(new Font("微软雅黑", Font.BOLD, 22));
        title.setForeground(new Color(199, 21, 133));
        title.setHorizontalAlignment(SwingConstants.CENTER);

        // 三个按钮
        JButton studentBtn = new JButton("学生");
        JButton teacherBtn = new JButton("教师");
        JButton adminBtn = new JButton("管理员");

        Color btnColor = new Color(255, 105, 180);
        JButton[] buttons = {studentBtn, teacherBtn, adminBtn};
        for (JButton btn : buttons) {
            btn.setBackground(btnColor);
            btn.setForeground(Color.WHITE);
            btn.setFont(new Font("微软雅黑", Font.BOLD, 18));
            btn.setFocusPainted(false);
            btn.setPreferredSize(new Dimension(100, 50));
        }

        // 布局
        JPanel btnPanel = new JPanel();
        btnPanel.setOpaque(false);
        btnPanel.add(studentBtn);
        btnPanel.add(teacherBtn);
        btnPanel.add(adminBtn);

        setLayout(new BorderLayout(10, 20));
        add(title, BorderLayout.NORTH);
        add(btnPanel, BorderLayout.CENTER);

        // 点击事件，弹登录框
        studentBtn.addActionListener(e -> new LoginDialog(this, "学生").setVisible(true));
        teacherBtn.addActionListener(e -> new LoginDialog(this, "教师").setVisible(true));
        adminBtn.addActionListener(e -> new LoginDialog(this, "管理员").setVisible(true));
    }

    // 登录弹窗
    static class LoginDialog extends JDialog {
        private JTextField userField;
        private JPasswordField passField;

        // 简单写死的账户密码示例
        // 账号和密码均为字符串
        private final String studentUser = "student1";
        private final String studentPass = "stu123";

        private final String teacherUser = "teacher1";
        private final String teacherPass = "tea123";

        private final String adminUser = "admin1";
        private final String adminPass = "adm123";

        public LoginDialog(JFrame parent, String role) {
            super(parent, role + "登录", true);
            setSize(350, 220);
            setLocationRelativeTo(parent);
            getContentPane().setBackground(new Color(255, 182, 193));
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10);

            JLabel userLabel = new JLabel("账号:");
            userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST;
            add(userLabel, gbc);

            userField = new JTextField(15);
            gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
            add(userField, gbc);

            JLabel passLabel = new JLabel("密码:");
            passLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.EAST;
            add(passLabel, gbc);

            passField = new JPasswordField(15);
            gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
            add(passField, gbc);

            JButton loginBtn = new JButton("登录");
            gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 2;
            gbc.anchor = GridBagConstraints.CENTER;
            add(loginBtn, gbc);

            // 登录验证
            loginBtn.addActionListener(e -> {
                String user = userField.getText().trim();
                String pass = new String(passField.getPassword()).trim();
                boolean success = false;

                switch (role) {
                    case "学生":
                        if (user.equals(studentUser) && pass.equals(studentPass)) success = true;
                        break;
                    case "教师":
                        if (user.equals(teacherUser) && pass.equals(teacherPass)) success = true;
                        break;
                    case "管理员":
                        if (user.equals(adminUser) && pass.equals(adminPass)) success = true;
                        break;
                }

                if (success) {
                    JOptionPane.showMessageDialog(this, role + "登录成功！");
                    dispose(); // 关闭登录窗口
                } else {
                    JOptionPane.showMessageDialog(this, "账号或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                    passField.setText("");
                }
            });
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new RoleSelectFrame().setVisible(true);
        });
    }
}
